package com.pur.zsms.storemanage.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.fileupload.DiskFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ExcelReader;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.ordercustommanage.vo.ExcelVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.storemanage.po.StoreInDetailedPo;
import com.pur.zsms.storemanage.po.StoreInPo;
import com.pur.zsms.storemanage.po.StoreMaterDetailPo;
import com.pur.zsms.storemanage.po.StoreMaterPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedValuePo;
import com.pur.zsms.storemanage.po.StoreOutPo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInDetailedVo;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutDetailedVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.storemanage.vo.StoreSupShopingVo;
import com.pur.zsms.storemanage.vo.StoreVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;

@Controller
@RequestMapping(value = "/storeSupplier")
public class StoreControllorForSupplier {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(StoreControllorForSupplier.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "storeService")
	private IStoreService storeService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	/**
	 * 供货商查看界面 仓库材料
	 */
	@RequestMapping(value = "/findSupplierStoreMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findSupplierStoreMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreMaterVo storeMaterVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		// 设置查询条件 ----下
		UserPo userInfo = (UserPo) session.getAttribute("user");
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		int storeHouseId = supplierService.getSupStoreHouseIdByUserId(supId);

		storeMaterVo.setStoreHouseId(storeHouseId);
		// 设置查询条件 ----上

		this.pageList = storeService.findSupplierStoreMater(pageController, storeMaterVo);
		List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreMaterVo f = new StoreMaterVo();
				f.setSid((Integer) item[0]);
				f.setStoreHouseId((Integer) item[1]);
				f.setMaterId((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setBrands((String) item[4]);
				f.setSpecifications((String) item[5]);
				f.setMaterNum((BigDecimal) item[6]);
				f.setStoreHouseName((String) item[7]);
				f.setMaterName((String) item[8]);
				f.setUnit((String) item[9]);
				f.setMaterValue((BigDecimal) item[10]);
				f.setMaterType((Integer) item[11]);
				f.setMaterValueDetail((String) item[12]);

				storeMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findSupplierStoreMaterForMinStore.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findSupplierStoreMaterForMinStore(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreMaterVo storeMaterVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		// 设置查询条件 ----下
		UserPo userInfo = (UserPo) session.getAttribute("user");
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		int storeHouseId = supplierService.getSupStoreHouseIdByUserId(supId);

		storeMaterVo.setStoreHouseId(storeHouseId);
		// 设置查询条件 ----上

		this.pageList = storeService.findSupplierStoreMaterForMinStore(pageController, storeMaterVo);

		List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreMaterVo f = new StoreMaterVo();
				f.setSid((Integer) item[0]);
				f.setStoreHouseId((Integer) item[1]);
				f.setMaterId((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setBrands((String) item[4]);
				f.setSpecifications((String) item[5]);
				f.setMaterNum((BigDecimal) item[6]);
				f.setStoreHouseName((String) item[7]);
				f.setMaterName((String) item[8]);
				f.setUnit((String) item[9]);
				f.setMaterType((Integer) item[10]);
				f.setMaterValueDetail((String) item[11]);
				storeMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findMaterByDateAndMaterType.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findMaterByDateAndMaterType(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			StoreMaterVo storeMaterVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		page = "1";
		rows = "999";

		if (storeMaterVo.getInitFlag() == 0) {
			json.put("success", false);
			return json.toString();
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		// 设置查询条件 ----下
		UserPo userInfo = (UserPo) session.getAttribute("user");
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());

		// 设置查询条件 ----上
		if (storeMaterVo.getInitFlag() == 1) {
			storeMaterVo.setSupId(supId);
			this.pageList = storeService.findMaterByOrderDateAndSupplierMaterType(pageController, storeMaterVo);
		} else if (storeMaterVo.getInitFlag() == 2) {
			storeMaterVo.setSupId(supId);
			this.pageList = storeService.findMaterByOrderDateAndSupplierMaterType(pageController, storeMaterVo);
		} else if (storeMaterVo.getInitFlag() == 3) {
			this.pageList = storeService.findMaterByOrderDateAndSupplierMaterType3(pageController, storeMaterVo);
		}
		List<StoreMaterVo> storeMaterVoList = new ArrayList<StoreMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreMaterVo f = new StoreMaterVo();
				// f.setSid((Integer) item[0]);
				// f.setStoreHouseId((Integer) item[1]);
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterNum(new BigDecimal(item[2].toString()));
				f.setUnit((String) item[3]);
				f.setBrands((String) item[4]);
				f.setSpecifications((String) item[5]);
				f.setMaterType((Integer) item[6]);
				// f.setMaterLevel((Integer) item[3]);
				storeMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/addStoreTemporaryWarehouseWarrant.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addShoppingCart(StoreSupShopingVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		/*
		 * try { UserPo userInfo = (UserPo) session.getAttribute("user"); int
		 * supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		 * c.setSupId(supId);
		 * 
		 * //boolean flag = materService.addShoppingCartSup(c); if (flag) {
		 * json.put("success", true); } else { json.put("success", false);
		 * json.put("msg", materService.getMsg()); } } catch (Exception e) {
		 * e.printStackTrace(); json.put("success", false); }
		 */
		return json.toString();
	}

	@RequestMapping(value = "/addStoreInForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addStoreInForSup(StoreInVo storeInVo, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int storeHouseId = storeService.getStoreHouseIdBySupId(supId);

			storeInVo.setStoreHouseId(storeHouseId);
			storeInVo.setStoreInState(1);// 状态未确认
			storeInVo.setStoreInWay(1);// 手动入库
			storeInVo.setOrdId(0);
			storeInVo.setSupId(supId);// 供货商
			storeInVo.setSupSupId(storeInVo.getSupSupId());

			StoreInPo storeInPo = storeService.addStoreInForSup(storeInVo);
			storeInVo.setStoreInId(storeInPo.getStoreInId());
			storeService.addStoreInDetailedForSup(storeInVo, storeInPo);
			// 改变总价格

			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/findStoreIn.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreIn(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, StoreInVo storeInVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		UserPo userInfo = (UserPo) session.getAttribute("user");
		storeInVo.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = storeService.listStoreInForSupplier(pageController, storeInVo);
		List<StoreInVo> storeInVoList = new ArrayList<StoreInVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreInVo f = new StoreInVo();
				f.setStoreInId((Integer) item[0]);
				// f.setStoreHouseId((Integer) item[1]);
				// f.setStoreName((String) item[2]);
				// f.setCustId((Integer) item[3]);
				// f.setCustName((String) item[4]);
				f.setSupSupId((Integer) item[1]);
				f.setSupName((String) item[2]);
				// f.setRemark((String) item[7]);
				f.setStoreInWay((Integer) item[3]);
				f.setStoreInState((Integer) item[4]);
				// f.setOrdId((Integer) item[10]);
				f.setCreatDate((String) item[5]);
				f.setMaterValueAll((BigDecimal) item[6]);

				f.setContent("查看入库详细内容");

				storeInVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeInVoList);
		return json.toString();
	}

	/**
	 * 进入入库详细订单
	 * */
	@RequestMapping(value = "/goStoreInDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goStoreInDetailed(int storeInId, String materDay) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		StoreInPo tStoreInPo = new StoreInPo();
		// 取得状态
		try {
			tStoreInPo = storeService.getStoreInPo(storeInId);
		} catch (GenericException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		map.put("storeInId", storeInId);
		map.put("materDay", materDay);
		map.put("storeInState", tStoreInPo.getStoreInState());
		return new ModelAndView("/storemanage/supplier/viewReportStoreInSupplierDetailed", map);
	}

	/**
	 * 入库审核
	 */
	@RequestMapping(value = "/reviewStoreIn.do", method = { RequestMethod.POST })
	@ResponseBody
	public String reviewStoreIn(StoreInDetailedVo storeInDetailedVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {

			JSONArray array = JSONArray.fromObject(storeInDetailedVo.getMaterdategridList());
			List<StoreInDetailedPo> storeInDetailedPoList = new ArrayList<StoreInDetailedPo>();
			String storeInId = "";

			String materDay = "";
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				StoreInDetailedPo tStoreInDetailedPo = new StoreInDetailedPo();
				storeInId = object.get("storeInId").toString();
				// tStoreInDetailedPo.setId(Integer.valueOf(object.get("id").toString()));
				tStoreInDetailedPo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tStoreInDetailedPo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tStoreInDetailedPo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tStoreInDetailedPo.setMaterValueUnit(new BigDecimal(object.get("materValueUnit").toString()));
				materDay = object.get("materDay").toString();

				storeInDetailedPoList.add(tStoreInDetailedPo);
			}

			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int hostid = storeService.getStoreHouseIdBySupId(supId);
			// storeInDetailedPoList =
			// storeService.getStoreInDetailedPoList(storeInId);
			storeService.updateStoreInMaterDetailForReview(storeInDetailedPoList, hostid, materDay);
			// storeService.updateStoreMater(storeInDetailedPoList, userInfo);

			// storeService.updateStoreMater(storeInDetailedPoList, userInfo);
			// 改变入库订单状态 已确认 并修改入库详细订单的 实际进货数量
			storeService.updateStoreInForReview(storeInId, storeInDetailedPoList);

			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/pulldownStoreOutWay.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownStoreOutWay(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = storeService.pulldownStoreOutWay(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	// 生成出库单
	@RequestMapping(value = "/minStoreOutForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String minStoreOutForSup(StoreOutVo storeOutVo, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int storeHouseId = storeService.getStoreHouseIdBySupId(supId);

			storeOutVo.setStoreHouseId(storeHouseId);
			storeOutVo.setStoreOutState(1);// 状态未确认
			// storeOutVo.setStoreOutWay(1);// 手动入库
			storeOutVo.setOrdId(0);
			storeOutVo.setSupId(supId);// 供货商
			storeOutVo.setSupSupId(storeOutVo.getSupSupId());

			// StoreInPo storeInPo = storeService.addStoreInForSup(storeOutVo);
			// storeOutVo.setStoreInId(storeInPo.getStoreInId());
			// storeService.addStoreInDetailedForSup(storeOutVo, storeInPo);

			StoreOutPo storeOutPo = storeService.minStoreOutForSup(storeOutVo);
			storeOutVo.setStoreOutId(storeOutPo.getStoreOutId());
			storeService.minStoreOutDetailedForSup(storeOutVo.getMaterdategridList(), storeOutPo);

			// 改变总价格
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "deleteStoreIn.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteStoreIn(StoreInVo c) {
		JSONObject json = new JSONObject();
		try {
			storeService.deleteStoreIn(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "deleteStoreOut.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteStoreOut(StoreOutVo c) {
		JSONObject json = new JSONObject();
		try {
			storeService.deleteStoreOut(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "getStoreInDetail.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getStoreInDetail(int storeInId) {
		JSONObject json = new JSONObject();
		try {
			List<StoreInDetailedVo> list = storeService.getStoreInDetailedVoList(storeInId);
			json.put("StoreInDetailedVoList", list);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/updateStoreInForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateStoreInForSup(StoreInVo storeInVo, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int storeHouseId = storeService.getStoreHouseIdBySupId(supId);

			StoreInPo tStoreInPo = storeService.getStoreInPo(storeInVo.getStoreInId());
			// 判断订单状态
			if (tStoreInPo.getStoreInState() != 1) {
				json.put("success", false);
				json.put("msg", "不能修改已被审核的订单");
			} else {
				BeanCopier copier = BeanCopier.create(StoreInPo.class, StoreInVo.class, false);
				copier.copy(tStoreInPo, storeInVo, null);
				storeInVo.setStoreHouseId(storeHouseId);
				storeInVo.setStoreInState(1);// 状态未确认
				storeInVo.setStoreInWay(1);// 手动入库
				storeService.deleteStoreIndetail(storeInVo);
				storeService.addStoreInDetailedForSup(storeInVo, tStoreInPo);
				json.put("success", true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/findStoreOut.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreOut(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, StoreOutVo storeOutVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		UserPo userInfo = (UserPo) session.getAttribute("user");
		storeOutVo.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = storeService.listStoreOutForSupplier(pageController, storeOutVo);
		List<StoreOutVo> storeOutVoList = new ArrayList<StoreOutVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreOutVo f = new StoreOutVo();
				f.setStoreOutId((Integer) item[0]);
				// f.setStoreHouseId((Integer) item[1]);
				// f.setStoreName((String) item[2]);
				// f.setCustId((Integer) item[3]);
				// f.setCustName((String) item[4]);
				// f.setSupSupId((Integer) item[1]);
				// f.setSupName((String) item[2]);
				// f.setRemark((String) item[7]);
				f.setStoreOutWay((Integer) item[1]);
				f.setStoreOutState((Integer) item[2]);
				// f.setOrdId((Integer) item[10]);
				f.setCreatDate((String) item[3]);
				f.setStoreOutValue((BigDecimal) item[4]);
				f.setOrdId((Integer) item[5]);
				f.setRemark((String) item[6]);
				f.setCustId((Integer) item[7]);
				if (item[8] != null) {
					f.setCustName((String) item[8]);
				} else {
					f.setCustName("无");
				}
				if (item[9] != null) {
					f.setSupSupName((String) item[9]);
				}
				f.setContent("查看入库详细内容");

				storeOutVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeOutVoList);
		return json.toString();
	}

	@RequestMapping(value = "/storeOutWayFormate.do", method = { RequestMethod.POST })
	public void storeOutWayFormate(String codetype, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter writer;
		try {
			String materTypeString = dataDictionaryService.getCodeJsonByCodeType(codetype);
			writer = response.getWriter();
			writer.write(materTypeString);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GenericException e) {
			e.printStackTrace();
		}
		// return json.toString();
	}

	/**
	 * 进入出库详细订单
	 * */
	@RequestMapping(value = "/goStoreOutDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goStoreOutDetailed(String storeOutId, String materDay) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		StoreOutPo tStoreOutPo = new StoreOutPo();
		// 取得状态
		try {
			tStoreOutPo = storeService.getStoreOutPoByStoreOutId(storeOutId);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		map.put("storeOutId", storeOutId);
		map.put("materDay", materDay);
		map.put("storeOutState", tStoreOutPo.getStoreOutState());
		return new ModelAndView("/storemanage/supplier/viewReportStoreOutSupplierDetailed", map);
	}

	/**
	 * 出库审核
	 */
	@RequestMapping(value = "/reviewStoreOut.do", method = { RequestMethod.POST })
	@ResponseBody
	public String reviewStoreOut(StoreOutDetailedVo storeOutDetailedVo, HttpSession session) {

		JSONObject json1 = new JSONObject();
		try {

			JSONArray array = JSONArray.fromObject(storeOutDetailedVo.getMaterdategridList());
			List<StoreOutDetailedPo> storeOutDetailedPoList = new ArrayList<StoreOutDetailedPo>();
			String storeOutId = "";

			String materDay = "";
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				StoreOutDetailedPo tStoreOutDetailedPo = new StoreOutDetailedPo();
				storeOutId = object.get("storeOutId").toString();
				// tStoreInDetailedPo.setId(Integer.valueOf(object.get("id").toString()));
				tStoreOutDetailedPo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				tStoreOutDetailedPo.setMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tStoreOutDetailedPo.setMaterNum(new BigDecimal(object.get("materNum").toString()));
				tStoreOutDetailedPo.setMaterValueUnit(new BigDecimal(object.get("materValueUnit").toString()));
				materDay = object.get("materDay").toString();

				storeOutDetailedPoList.add(tStoreOutDetailedPo);
			}

			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int hostid = storeService.getStoreHouseIdBySupId(supId);
			// storeInDetailedPoList =
			// storeService.getStoreInDetailedPoList(storeInId);
			// storeService.updateStoreInMaterDetail(storeOutDetailedPoList,
			// hostid, materDay);

			storeService.updateStoreOutMaterDetailForReview(storeOutDetailedPoList, hostid, materDay);

			// storeService.updateStoreMater(storeInDetailedPoList, userInfo);

			// storeService.updateStoreMater(storeInDetailedPoList, userInfo);
			// 改变入库订单状态 已确认 并修改入库详细订单的 实际进货数量

			storeService.updateStoreOutForReview(storeOutId, storeOutDetailedPoList);

			json1.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}
}
